package com.shinhye.mobilelottery.ui;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shinhye.mobilelottery.ConstantValue;
import com.shinhye.mobilelottery.R;
import com.shinhye.mobilelottery.engine.CommonInfoEngine;
import com.shinhye.mobilelottery.net.protocol.Element;
import com.shinhye.mobilelottery.net.protocol.Message;
import com.shinhye.mobilelottery.net.protocol.Oelement;
import com.shinhye.mobilelottery.net.protocol.element.IssueInfoElement;
import com.shinhye.mobilelottery.utils.BeanFactory;
import com.shinhye.mobilelottery.utils.LogUtil;
import com.shinhye.mobilelottery.utils.PromptManager;
import com.shinhye.mobilelottery.utils.TimeUtils;

public class HallUI1 extends BaseUI{
	private TextView summary;

	public HallUI1(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate() {
		LinearLayout showView = (LinearLayout) View.inflate(getContext(), R.layout.il_hall1, null);
		summary = (TextView) showView.findViewById(R.id.ii_hall_ssq_summary);
		setContentView(showView);
	}
	
	private void getIssueInfo() {
		new MyAsyncTask<Integer>() {
			@Override
			protected Message doInBackground(Integer... params) {
				int id = params[0];
				CommonInfoEngine infoEngine = BeanFactory.getImpl(CommonInfoEngine.class);
				Message res = infoEngine.getIssuesInfo(id);
				return res;
			}

			@Override
			protected void onPostExecute(Message result) {
				if(result == null)
					return;
				
				Oelement oelement = result.getBody().getOelement();
				if(ConstantValue.SUCCESS.equals(oelement.getErrorcode())) {
					changeNotice(result.getBody().getElements().get(0));
				} else {
					PromptManager.showErrorDialog(getContext(), oelement.getErrormsg());
				}
			}
		}.execute(ConstantValue.SSQ);
	}
	
	private void changeNotice(Element element) {
		IssueInfoElement infoEle = (IssueInfoElement) element;
		String issue = infoEle.getIssue().getValue();
		String lasttime = TimeUtils.getStandardTime(infoEle.getLasttime().getValue());
		String text=getContext().getResources().getString(R.string.is_hall_common_summary);
		text=StringUtils.replaceEach(text, new String[]{"ISSUE","TIME"}, new String[]{issue,lasttime});
		summary.setText(text);
	}
	
	@Override
	public int getId() {
		return ConstantValue.VIEW_HALL;
	}

	@Override
	protected void setListener() {
		
	}

	@Override
	public void onResume() {
		LogUtil.info(HallUI1.class, "onResume");
		getIssueInfo();
	}

	@Override
	public void onPause() {
		LogUtil.info(HallUI1.class, "onPause");
	}
	
}
